<template>
  <div class="sidebar">
    <el-menu
        class="sidebar-el-menu"
        :default-active="onRoutes"
        :collapse="collapse"
        background-color="white"
        text-color="black"
        active-text-color="#20a0ff"
        unique-opened
        router
    >
      <el-menu-item index="/dashboard">
        <i class="el-icon-s-home"></i>
        <span slot="title">首页</span>
      </el-menu-item>

      <el-submenu :key="root.id" :index="root.id+''" v-for="root in rootList">
        <template slot="title">
          <i :class="root.authorityIco"></i>
          <span slot="title">{{root.authorityName}}</span>
        </template>
        <template v-for="menu in root.children">
          <template v-if="menu.children.length!=0">
            <el-submenu :key="menu.id" :index="menu.id+''">
              <template slot="title">
                <i :class="menu.authorityIco"></i>
                <span slot="title">{{menu.authorityName}}</span>
              </template>

              <el-menu-item-group>
                <el-menu-item :key="child.id+''" :index="child.authorityPath" v-for="child in menu.children">
                  <i :class="child.authorityIco"></i>
                  <template slot="title">{{child.authorityName}}</template>
                </el-menu-item>
              </el-menu-item-group>
            </el-submenu>
          </template>

          <template v-else>
            <el-menu-item-group :index="menu.id+''" :key="menu.id">
              <el-menu-item :index="menu.authorityPath">
                <i :class="menu.authorityIco"></i>
                <template slot="title">{{menu.authorityName}}</template>
              </el-menu-item>
            </el-menu-item-group>
          </template>
        </template>
      </el-submenu>


    </el-menu>
  </div>



</template>

<script>
import bus from '../../../utils/bus.js';
export default {
  data() {
    return {
      collapse: false
    };
  },
  computed: {
    onRoutes() {
      // return this.$route.path.replace('/', '');
      return location.href.split('#/')[1]
    },
    rootList:{
      get(){
        return this.$store.state.menu
      }
    }
  },
  created() {
    // 通过 Event Bus 进行组件间通信，来折叠侧边栏
    bus.$on('collapse', msg => {
      this.collapse = msg;
      bus.$emit('collapse-content', msg);
    });
  }
};
</script>

<style scoped>
.sidebar {
  display: block;
  position: absolute;
  left: 0;
  top: 70px;
  bottom: 0;
  overflow-y: scroll;
}
.sidebar::-webkit-scrollbar {
  width: 0;
}
.sidebar-el-menu:not(.el-menu--collapse) {
  width: 250px;
}
.sidebar > ul {
  height: 100%;
}
</style>
